// app.js
App({
  onLaunch() {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync()
    this.globalData.statusBarHeight = systemInfo.statusBarHeight
    this.globalData.screenWidth = systemInfo.screenWidth
    this.globalData.screenHeight = systemInfo.screenHeight
    this.globalData.safeArea = systemInfo.safeArea
    
    // 检查更新
    this.checkUpdate()
    
    // 检查登录状态
    this.checkLoginStatus()

    // 初始化模拟用户数据
    this.initMockData()
  },

  globalData: {
    userInfo: null,
    statusBarHeight: 0,
    screenWidth: 0,
    screenHeight: 0,
    safeArea: null,
    isLogin: false,
    mockUsers: [],
    baseUrl: 'http://localhost:8080',
    appId: 'wx19091684c8042cec',
    // 生产环境API地址
    // baseUrl: 'https://你的域名/flower_shop'
  },

  initMockData() {
    // 模拟用户数据
    const mockUsers = [
      {
        id: 1,
        nickName: '测试用户1',
        avatarUrl: '/images/default-avatar.png',
        gender: 1
      },
      {
        id: 2,
        nickName: '测试用户2',
        avatarUrl: '/images/default-avatar.png',
        gender: 2
      }
    ]
    this.globalData.mockUsers = mockUsers
  },

  checkUpdate() {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate((res) => {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: (res) => {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
        }
      })
    }
  },

  checkLoginStatus() {
    // 检查用户登录状态
    const token = wx.getStorageSync('token')
    const userInfo = wx.getStorageSync('userInfo')
    
    // 检查商家登录状态
    const merchantToken = wx.getStorageSync('merchant_token')
    const merchantInfo = wx.getStorageSync('merchantInfo')
    
    // 设置用户登录状态
    if (token && userInfo) {
      this.globalData.userInfo = userInfo
      this.globalData.isLogin = true
    } else {
      // 清除可能存在的无效数据
      wx.removeStorageSync('token')
      wx.removeStorageSync('userInfo')
      this.globalData.userInfo = null
      this.globalData.isLogin = false
    }
    
    // 设置商家登录状态 - 添加新的全局变量
    if (merchantToken && merchantInfo) {
      this.globalData.merchantInfo = merchantInfo
      this.globalData.isMerchantLogin = true
    } else {
      // 清除可能存在的无效商家数据
      wx.removeStorageSync('merchant_token')
      wx.removeStorageSync('merchantInfo')
      this.globalData.merchantInfo = null
      this.globalData.isMerchantLogin = false
    }
  },

  updateLoginStatus(isLogin, userInfo = null) {
    this.globalData.isLogin = isLogin
    this.globalData.userInfo = userInfo
    
    if (isLogin && userInfo) {
      wx.setStorageSync('userInfo', userInfo)
    } else {
      wx.removeStorageSync('token')
      wx.removeStorageSync('userInfo')
    }
  },

  showToast(title, icon = 'none') {
    wx.showToast({
      title,
      icon,
      duration: 2000
    })
  },

  showLoading(title = '加载中...') {
    wx.showLoading({
      title,
      mask: true
    })
  },

  hideLoading() {
    wx.hideLoading()
  },

  // 模拟登录
  mockLogin(userInfo) {
    return new Promise((resolve) => {
      setTimeout(() => {
        // 模拟服务器响应
        const mockUser = {
          ...this.globalData.mockUsers[0],
          ...userInfo
        }
        const token = 'mock_token_' + Date.now()
        
        // 保存到全局数据和本地存储
        this.globalData.userInfo = mockUser
        this.globalData.isLogin = true
        wx.setStorageSync('token', token)
        wx.setStorageSync('userInfo', mockUser)
        
        resolve({
          code: 0,
          data: {
            token,
            userInfo: mockUser
          },
          msg: '登录成功'
        })
      }, 500) // 模拟网络延迟
    })
  },

  // 添加评价页面的全局方法
  gotoReview: function(orderNo) {
    wx.navigateTo({
      url: `/pages/review/review?orderNo=${orderNo}`
    })
  }
})
